﻿using Aspose.Cells.Drawing;
using Estsh.Core.Base;
using Estsh.Core.Model.Extend.Models;
using Estsh.Core.Model.Result;
using Estsh.Core.Models;
using Estsh.Core.Services;
using Estsh.Core.Util;
using Estsh.Core.Wms.IRepositories;
using Estsh.Core.Wms.IServices;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Estsh.Core.Wms.Services
{
    public class ASPInterfaceService : BaseService<BaseEntity>, IASPInterfaceService
    {
        private readonly IAPSInterfaceRepository repository;
        public ASPInterfaceService(IAPSInterfaceRepository _repository) : base(_repository)
        {
            repository = _repository;
        }

        /*
        public Hashtable GetAPSMenuInfos(int userId, int menuId)
        {

            List<SysWebMenu> menus = repository.getMainMenuListByUser(userId, menuId);
            Hashtable result = new Hashtable();
            List<Hashtable> dataList = new List<Hashtable>();
            Hashtable node;

            foreach (var menu in menus)
            {
                node = new Hashtable();
                node.Add("id", menu.MenuId);
                node.Add("name", menu.Name != null ? menu.Name : "");
                node.Add("className", menu.IconSkin != null ? menu.IconSkin : "");

              
                List<Hashtable> _treeNodes = new List<Hashtable>();
                Hashtable _node;
                List<SysWebMenu> _menus = repository.getMainMenuListByUser(userId, menu.MenuId);
                foreach (var m in _menus)
                {
                    _node = new Hashtable();
                    _node.Add("id", m.MenuId.ToString());
                    _node.Add("name", m.Name != null ? m.Name : "");
                    _node.Add("parentId", m.ParentId.ToString());
                    _node.Add("icon", m.IconUrl != null ? m.IconUrl : "");
                    _node.Add("tabUrl", (m.Url != null ? m.Url : ""));
                    _node.Add("target", "frmright");
                  
                    List<TreeNode> treeNodesOp = new List<TreeNode>();//按钮
                    TreeNode _nodeOp;
                    List<SysProgramFunOp_Extend> _menusOp = repository.getProgramFunOpListByUser(userId, m.MenuId);
					if (_menusOp.Count > 0)
					{
						foreach (var po in _menusOp)
						{
                            _nodeOp = new TreeNode();
                            _nodeOp.id = po.OpNum.ToString();
                            _nodeOp.name = po.OpName != null ? po.OpName : "";
                            _nodeOp.parentId = po.FunName.ToString();
                            _nodeOp.icon = po.OpClass != null ? po.OpClass : "";
                            _nodeOp.tabUrl = "";
                            _nodeOp.target = "frmright";
                            treeNodesOp.Add(_nodeOp);
                        }
                        _node.Add("childList", treeNodesOp);
                    }

                    _treeNodes.Add(_node);
                }

                node.Add("childList", _treeNodes);
                dataList.Add(node);
                //result.Add("treeNodes", treeNodes);
            }
            result.Add("list", dataList);
            return result;

        }
        */


        public Hashtable GetAPSMenuInfos(int userId, int menuId)
        {

            List<SysWebMenu> menus = repository.getMainMenuListByUser(userId, menuId);
            Hashtable result = new Hashtable();
            List<Hashtable> dataList = new List<Hashtable>();
            Hashtable node;
            List<TreeNode> treeNodesOp = new List<TreeNode>();//按钮
            foreach (var menu in menus)//父菜单
            {
                node = new Hashtable();
                node.Add("id", menu.MenuId);
                node.Add("name", menu.Name != null ? menu.Name : "");
                node.Add("className", menu.IconSkin != null ? menu.IconSkin : "");
                node.Add("parentId", menu.ParentId.ToString());

                List<Hashtable> _treeNodes = new List<Hashtable>();
                Hashtable _node;
                List<SysWebMenu> _menus = repository.getMainMenuListByUser(userId, menu.MenuId);
                foreach (var m in _menus)//子菜单
                {
                    _node = new Hashtable();
                    _node.Add("id", m.MenuId.ToString());
                    _node.Add("name", m.Name != null ? m.Name : "");
                    _node.Add("parentId", m.ParentId.ToString());
                    _node.Add("icon", m.IconUrl != null ? m.IconUrl : "");
                    _node.Add("tabUrl", (m.Url != null ? m.Url : ""));
                    _node.Add("target", "frmright");


                    TreeNode _nodeOp;
                    List<SysProgramFunOp_Extend> _menusOp = repository.getProgramFunOpListByUser(userId, m.MenuId);//按钮
                    if (_menusOp.Count > 0)
                    {
                        foreach (var po in _menusOp)
                        {
                            _nodeOp = new TreeNode();
                            _nodeOp.id = po.OpNum.ToString();
                            _nodeOp.name = po.OpName != null ? po.OpName : "";
                            _nodeOp.parentId = po.FunName.ToString();
                            _nodeOp.icon = po.OpClass != null ? po.OpClass : "";
                            _nodeOp.tabUrl = "";
                            _nodeOp.target = "frmright";
                            treeNodesOp.Add(_nodeOp);
                        }

                    }

                    _treeNodes.Add(_node);
                }


                node.Add("childList", _treeNodes);
                dataList.Add(node);
                //result.Add("treeNodes", treeNodes);
            }

            #region 子系统级按钮（aps系统）
            TreeNode oneNodeOp;
            List<SysProgramFunOp_Extend> oneMenusOp = repository.getProgramFunOpListByUser(userId, menuId);//按钮 
            if (oneMenusOp.Count > 0)
            {
                foreach (var po in oneMenusOp)
                {
                    oneNodeOp = new TreeNode();
                    oneNodeOp.id = po.OpNum.ToString();
                    oneNodeOp.name = po.OpName != null ? po.OpName : "";
                    oneNodeOp.parentId = po.FunName.ToString();
                    oneNodeOp.icon = po.OpClass != null ? po.OpClass : "";
                    oneNodeOp.tabUrl = "";
                    oneNodeOp.target = "frmright";
                    treeNodesOp.Add(oneNodeOp);
                }

            }

            #endregion






            result.Add("buttonList", treeNodesOp);
            result.Add("list", dataList);


            return result;

        }



    }
}
